﻿using MMS.Utility.Attrs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MMS.Utility;
using MMS.UI.Helper;

namespace MMS.UI.Areas.Admin.Controllers
{
    public class UserController : BaseController
    {
        //
        // GET: /Admin/User/
         [HttpGet, SkipLogin]
        public ActionResult Login()
        {
            return View();
        }
         #region 1.2 接收登录数据 + Login(MMS.UI.ViewModel.LoginModel userLoginModel)
         /// <summary>
         /// 1.2 接收登录数据
         /// </summary>
         /// <param name="form"></param>
         /// <returns></returns>
         [HttpPost, SkipLogin]
         public ActionResult Login(MMS.UI.Areas.Admin.ViewModels.LoginModel userLoginModel)
         {
             //a.做服务器端验证
             if (ModelState.IsValid)
             {
                 //b.检查验证码
                 if (Session[VCode.vCodeName] != null && userLoginModel.LoginCode.IsSame(Session[VCode.vCodeName].ToString()))
                 {
                     var user = OpeCur.GetAsync<Model.Employee>("http://localhost:16824/api/User/GetUserByName/" + userLoginModel.LoginName);
                     //c.1 登录名错误
                     if (user == null)
                     {
                         return OpeCur.AjaxMsgNOOK("登录名错误~~！");
                     }
                     //c.2如果登录名没错，则验证密码是否正确
                     else
                     {
                         //d.1 如果密码相等，则登陆成功
                         if (user.EmpLoginPwd.IsSame(userLoginModel.LoginPwd))
                         {
                             //e.1保存当前登陆用户对象到 Sessioin
                             OpeCur.UserNow = user.ToPOCO();//将EF查出来的 代理对象 转成 普通实体类对象保存 POCO
                             //e.2判断是否需要保存 Cookie
                             if (userLoginModel.IsKeepLogin)
                             {
                                 OpeCur.UserId = user.EmpId;
                             }
                             //f.1查询登录用户的权限集合 并存入 Session
                             OpeCur.UserNowPers = OpeCur.GetAsync<List<Model.Permission>>("http://localhost:16824/api/User/GetUserPermission/" + user.EmpId);
                       

                             return OpeCur.AjaxMsgOK("登录成功了~", "/admin/manage/index");
                         }
                         //d.2 登录失败
                         else
                         {
                             return OpeCur.AjaxMsgNOOK("登录密码错误~~！");
                         }
                     }
                 }
                 //b.1验证码错误
                 else
                 {
                     return OpeCur.AjaxMsgNOOK("验证码输入错误~~~");
                 }
             }
             return OpeCur.AjaxMsgNOOK("请不要关闭浏览器JS功能~~！");
         }
         #endregion
    }
}
